package com.cold.demo.jsoup.zhihu;


import org.springframework.util.StreamUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class HeaderResolve {
    private static Map<String, String> headers = new HashMap<>();

    static {
        String str;
        try {
            InputStream headersInputStream = new FileInputStream("D:\\Software\\IdeaProjects\\demo-project\\demo-jsoup\\src\\main\\java\\com\\cold\\demo\\jsoup\\zhihu\\headers.txt");

            str = StreamUtils.copyToString(headersInputStream, StandardCharsets.UTF_8);
        }catch (IOException e){
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }

        String[] split = str.split("\r\n");

        for (String h : split) {
            if (h == null || h.trim().isEmpty()) {
                continue;
            }
            boolean supplement;
            if (supplement = h.startsWith(":")) {
                h = h.replaceFirst(":", "");
            }
            int    i    = h.indexOf(":");
            String name = h.substring(0, i);
            if (supplement) name = ":" + name;
            String value = h.substring(i + 2);
            headers.put(name, value);
        }
    }

    public static Map<String,String> getHeaders(){
        return new HashMap<>(headers);
    }
}
